package com.kehutong.flow.service;

import com.kehutong.common.DoveClient;
import com.kehutong.common.util.Token;
import com.kehutong.flow.entity.FlowOffer;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.mvc.webservice.WebService;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;
import org.coraframework.util.Objects;

@WebService("/flow/service/offer")
public class FlowOfferService {

    private static final Logger logger = LoggerFactory.getLogger(FlowOfferService.class);

    @Inject
    private JdbcSession jdbcSession;

    @Inject
    private DoveClient doveClient;

    @ReqMapping("/list")
    public Page<FlowOffer> list(JSONObject jsonObject) {
        return jdbcSession.findPage(FlowOffer.class)
                .eq(jsonObject, "id", "communityNo", "roleNo", "employeeNo")
                .eq("deleted", false)
                .order("createTime", "desc")
                .page(jsonObject)
                .exe();
    }

    @ReqMapping("/get")
    public Object get(String id) {
        final FlowOffer offer = Pool.get(FlowOffer.class, id);
        if (Objects.nonNull(offer) && offer.isDeleted()) {
            return Result.success();
        }

        return offer;
    }

    @Bean(newInstance=true, copy=true)
    @ReqMapping("/save")
    public Object save(FlowOffer item) {
        if (hasOffer(item)) {
            return Result.error(0, "已有该岗位！");
        }

        jdbcSession.insert(item);
        return Result.success();
    }

    @Bean(copy=true)
    @ReqMapping("/update")
    public Object update(FlowOffer item) {
        if (hasOffer(item)) {
            return Result.error(0, "已有该岗位！");
        }

        jdbcSession.updateById(item);
        return Result.success();
    }

    @ReqMapping("/delete")
    public Object delete(String id) {
        final FlowOffer role = Pool.get(FlowOffer.class, id);
        if (Objects.nonNull(role)) {
            role.setDeleted(true);
            jdbcSession.updateById(role);
        }

        return Result.success();
    }

    @ReqMapping("/getEmp")
    public Object getEmp(JSONObject jsonObject, Token token) throws Exception {
        JSONObject object = doveClient.post("/repairs/admin/service/get", (http) -> {
            http.addHeader("companyNo", token.getCompanyNo());
            http.addParam("id", jsonObject.getString("id"));
        });

        JSONObject repairs;
        if (object.getInteger("code") == 200) {
            repairs = object.getJSONObject("data");
        } else {
            logger.error("getEmp: {}", object.getString("message"));
            return Result.error(0, "对象不存在");
        }

        Page<FlowOffer> page = jdbcSession.findPage(FlowOffer.class)
                .eq("communityNo", repairs.getString("communityNo"))
                .eq("deleted", false).page(jsonObject).exe();
        return Result.success(page);
    }

    private boolean hasOffer(FlowOffer item) {
        FlowOffer offer = jdbcSession.findOne(FlowOffer.class)
                .eq("communityNo", item.getCommunityNo())
                .eq("employeeNo", item.getEmployeeNo())
                .eq("roleNo", item.getRoleNo())
                .eq("deleted", false)
                .exe();

        if (Objects.isNull(offer)) {
            return false;
        }

        if (item.getId().equals(offer.getId())) {
            return false;
        }

        return true;
    }

}
